class GuaAnimation {
    constructor(game) {
        self.game = game
        this.game = game
        this.addAnimation()
        this.setUp()

    }
    addAnimation() {
    }
    setUp() {
        var animationNames = Object.keys(this.animations)
        for (let i = 0; i < animationNames.length; i++) {
            let aName = animationNames[i]
            let animationCount = this.animationCount[aName]
            for (let j = 0; j < animationCount;j++) {
                var name = `${aName}${j}` 
                var t = GuaImage.new(this.game, name) 
                this.animations[aName].push(t)
            }
        }

        this.frames = this.animations['idle']
        this.texture = this.frames[0]
        this.frameIndex = 0
        this.frameCount = 3

        this.flipX = false
        this.rotation = 0
    }


    changeAnimation(animationName) {
        this.frames = this.animations[animationName]
    }

    move(x, y) {
        this.setPosition(this.x+x, this.y+y)
    }
    draw() {
        this.game.context.save()
        
        var centerX = this.texture.x + this.texture.w / 2
        var centerY = this.texture.y + this.texture.h / 2
        this.game.context.translate(centerX, centerY)
        if (this.flipX) {
            this.game.context.scale(-1, 1)
        }
        this.game.context.rotate(this.rotation * Math.PI / 180)
        this.game.context.translate(-this.texture.w / 2, - this.texture.h / 2)

        this.game.context.drawImage(this.texture.texture, 0, 0)

        this.game.context.restore()
    }


    update() {
        this.frameCount--
        if (this.frameCount == 0) {
            this.frameCount = 3
            this.frameIndex = (this.frameIndex + 1) % this.frames.length
            this.texture =  this.frames[this.frameIndex]
        }

    }
    setPosition(x, y) {
        this.x = x 
        this.y = y
       var names = Object.keys(this.animations) 
        for(let i = 0; i < names.length; i ++) {

            var key = names[i]
            var frames = this.animations[key]
            for (let j = 0; j < frames.length; j++) {
                frames[j].setPosition(x, y)
            }
        }
    }

}
